﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shohdi.DataHandler;
using System.Data;
using System.IO;
using System.Threading;

/// <summary>
/// Summary description for FileGarbishCollection
/// </summary>
public class FileGarbishCollection
{

    string _strConnection = "";

    public string strConnection
    {
        get { return _strConnection; }
        set { _strConnection = value; }
    }
    string _TableName = "";

    public string TableName
    {
        get { return _TableName; }
        set { _TableName = value; }
    }
    string _ColumnName = "";

    public string ColumnName
    {
        get { return _ColumnName; }
        set { _ColumnName = value; }
    }
    string _CurrentPath = "";

    public string CurrentPath
    {
        get { return _CurrentPath; }
        set { _CurrentPath = value; }
    }


    public static void startClearThread(List<FileGarbishCollection> lstParams)
    {

        Thread thNew = new Thread(new ParameterizedThreadStart(clearFilesThread));
        thNew.Start(lstParams);



    }




    private static void clearFilesThread(object obj)
    {
        List<FileGarbishCollection> lstParams = (List<FileGarbishCollection>)obj;

        while (true)
        {

            foreach (FileGarbishCollection lstNew in lstParams)
            {
                clearFiles(lstNew.strConnection, lstNew.TableName, lstNew.ColumnName, lstNew.CurrentPath);
                clearDirs(lstNew.strConnection, lstNew.TableName, lstNew.ColumnName, lstNew.CurrentPath);
            }
            Thread.Sleep(60000);
        }


    }



    private static void clearFiles(string strConnection, string tablename, string columnname, string path)
    {
        


            ClassDataHandler con = new ClassDataHandler(strConnection);
            con.OpenConnection();

            DataTable dtNew = con.SelectInDataTable(" select distinct " + columnname + " from " + tablename + " where (not " + columnname + " is null) AND " + columnname + " <> '' ", CommandType.Text);
            DataRow[] arrDr = dtNew.Select();

            con.CloseConnection();





            DirectoryInfo di = new DirectoryInfo(path);

            FileInfo[] arrFi = di.GetFiles();


            foreach (FileInfo fi in arrFi)
            {


                IEnumerable<DataRow> lstDr = from dr in arrDr where dr[columnname].ToString().ToLower().Contains(fi.Name.ToLower()) select dr;

                if (lstDr.Count() == 0)
                {
                    if (fi.CreationTime < DateTime.Now.AddHours(-1))
                    {
                        try
                        {
                            fi.Delete();
                        }
                        catch { }
                    }
                }


            }



        

        

    }


    private static void clearDirs(string strConnection, string tablename, string columnname, string path)
    {



        ClassDataHandler con = new ClassDataHandler(strConnection);
        con.OpenConnection();

        DataTable dtNew = con.SelectInDataTable(" select distinct " + columnname + " from " + tablename + " where (not " + columnname + " is null) AND " + columnname + " <> '' ", CommandType.Text);
        DataRow[] arrDr = dtNew.Select();

        con.CloseConnection();





        DirectoryInfo di = new DirectoryInfo(path);

        DirectoryInfo[] arrDi = di.GetDirectories();


        foreach (DirectoryInfo di1 in arrDi)
        {


            IEnumerable<DataRow> lstDr = from dr in arrDr where dr[columnname].ToString().ToLower().Contains(di1.Name.ToLower()) select dr;

            if (lstDr.Count() == 0)
            {
                if (di1.CreationTime < DateTime.Now.AddHours(-1))
                {
                    try
                    {
                        di1.Delete(true);
                    }
                    catch { }
                }
            }


        }







    }



	public FileGarbishCollection(string __strConnection,string __TableName,string __ColumnName,string __CurrentPath)
	{
        strConnection = __strConnection;
        TableName = __TableName;
        ColumnName = __ColumnName;
        CurrentPath = __CurrentPath;
       


		//
		// TODO: Add constructor logic here
		//
	}
}